package com.eqdd.yiqidian.ui.gongneng.renshi.gonggao;

import android.databinding.DataBindingUtil;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.eqdd.yiqidian.CreateTrumpetActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

public class CreateTrumpetActivity extends BaseActivity {

    private CreateTrumpetActivityCustom dataBinding;
    private String title;
    private String content;
    private int num;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_trumpet);
        title = getIntent().getStringExtra(Config.TITLE);
        content = getIntent().getStringExtra(Config.CONTENT);
        num = getIntent().getIntExtra(Config.MAX_NUM, -1);
        initTopTitleBar(View.VISIBLE, title);
        initTopRightText("确定", v -> {
            if (!TextUtils.isEmpty(dataBinding.etContent.getText())) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_TRUMPET)
                        .params("userGuid", user.getGuid())
                        .params("comid", user.getCompanyId())
                        .params("content", dataBinding.etContent.getText().toString())
                        .execute(new DialogCallBack<HttpResult>(CreateTrumpetActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort("请输入内容");
            }
        });


    }

    @Override
    public void initData() {

    }

    @Override
    public void setView() {

        dataBinding.etContent.setText(content);
        if (num != 1) {
            dataBinding.tvNum.setText("0/" + num);
            dataBinding.etContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(num)});
        }
        dataBinding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (num != -1) {
                    dataBinding.tvNum.setText(s.length() + "/" + num);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}
